import { Router } from 'express';
import enrollmentController from '../controllers/EnrollmentController.js';
import db from '../models/index.js';
import { protect, restrictTo } from '../middlewares/auth.js';

const { User } = db;

const router = Router();

// 所有路由都需要登录
router.use(protect);

// 学生选课
router.post(
  '/courses/:courseId/enroll',
  restrictTo(User.ROLES.STUDENT),
  enrollmentController.enrollCourse
);

// 学生退课
router.post(
  '/courses/:courseId/drop',
  restrictTo(User.ROLES.STUDENT),
  enrollmentController.dropCourse
);

// 获取我的选课列表（学生）
router.get(
  '/my-courses',
  restrictTo(User.ROLES.STUDENT),
  enrollmentController.getMyEnrollments
);

// 获取课程的学生列表（教师）
router.get(
  '/courses/:courseId/students',
  restrictTo(User.ROLES.TEACHER, User.ROLES.ADMIN),
  enrollmentController.getCourseEnrollments
);

// 给学生评分（教师）
router.patch(
  '/:enrollmentId/grade',
  restrictTo(User.ROLES.TEACHER, User.ROLES.ADMIN),
  enrollmentController.gradeStudent
);

// 获取我的成绩单（学生）
router.get(
  '/my-transcript',
  restrictTo(User.ROLES.STUDENT),
  enrollmentController.getMyTranscript
);

export default router;
